package com.china08.yxyapi.api.manage.tec.teaching.lecture.quesgroup;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.service.manage.tec.teaching.lecture.LectureQuestionService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

/**
 * 
 * 我的教学-资源列表-创建题组
 *
 */
@RestController
@CrossOrigin
public class LectureQuesSchController extends YxyApiBaseController {

    @Autowired
    private LectureQuestionService lectureQuestionService;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/quesgroup/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel)
                    throws JsonParseException, JsonMappingException, IOException {

        List<RespModel> list = new ArrayList<LectureQuesSchController.RespModel>();
        Pageable pageable = new PageRequest(reqModel.page, reqModel.size);
        String questionType = reqModel.questionType;
        String bookContent = reqModel.bookcontent;
        String bookId = reqModel.bookId;
        Page<ElasticYxyYxyQuesResource> page = null;
        List<String> quesTypeList = new ArrayList<String>();
        quesTypeList.add("1");
        quesTypeList.add("2");
        quesTypeList.add("8");
        if (questionType.equals("6")) {
            page = lectureQuestionService.questionFindByBookcontentAndLevelAndTypeAndQuestionTypeNotIn(bookContent,
                    bookId, reqModel.difficultyLevel, reqModel.type, quesTypeList, pageable);
        } else {
            page = lectureQuestionService.questionFindByBookcontentAndLevelAndTypeAndQuestionType(bookContent, bookId,
                    reqModel.difficultyLevel, reqModel.type, questionType, pageable);
        }

        for (ElasticYxyYxyQuesResource elasticYxyYxyQuesResource : page) {
            RespModel model = new RespModel();
            model.difficultyLevel = elasticYxyYxyQuesResource.getDifficultyLevel();
            model.id = elasticYxyYxyQuesResource.getId();
            model.knowledgeTexts = elasticYxyYxyQuesResource.getKnowledgeTexts();
            model.knowledgeIds = elasticYxyYxyQuesResource.getKnowledgeIds();
            model.quesBody = elasticYxyYxyQuesResource.getQuesBody();
            model.questionType = elasticYxyYxyQuesResource.getQuestionType();
            list.add(model);
        }

        Page<RespModel> resPage = new PageImpl<LectureQuesSchController.RespModel>(list, pageable,
                page.getTotalElements());

        return resPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String bookcontent;
        public String difficultyLevel;
        public String type;
        public String questionType;
        public String bookId;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(bookcontent, null), "请填写目录");
        }
    }

    protected static class RespModel {
        public String id;
        public String difficultyLevel;
        public List<String> knowledgeTexts;
        public List<Integer> knowledgeIds;
        public String questionType;
        public Object quesBody;
        public boolean favorite;
        public Long quesTimes;
    }
}
